/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package se392.ateam2006.authentication;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import se392.ateam2006.user.UserEntity;

/**
 *
 * @author Caroline
 */
@Entity
public class GroupEntity implements Serializable {

    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    //@JoinTable(name="USERGROUPS",
    //joinColumns=@JoinColumn(name="USER_ID",
    //        referencedColumnName="email"),
    //        inverseJoinColumns=@JoinColumn(
    //        name="GROUP_ID",
    //        referencedColumnName="permissions"))
    @JoinTable(name = "USERGROUPS", 
        joinColumns = {@JoinColumn(name = "USER_ID")}, 
        inverseJoinColumns = {@JoinColumn(name = "GROUP_ID")})
    private List<UserEntity> users = new ArrayList<UserEntity>();
    @Id
    private String groupid;

    public GroupEntity() {
    }

    public GroupEntity(String permission) {
        this.groupid = permission;

    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += groupid.hashCode();
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof GroupEntity)) {
            return false;
        }
        GroupEntity other = (GroupEntity) object;
        if (this.groupid.equals(other.groupid)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "se392.ateam2006.authentication.GroupEntity[id=" + groupid + "]";
    }

    public List<UserEntity> getUsers() {
        return users;
    }

    public void setUses(List<UserEntity> users) {
        this.users = users;
    }

    public void addUser(UserEntity user) {
        users.add(user);
    }

    public void removeUser(UserEntity user) {
        users.remove(user);
    }

    public void removeAllUsers() {
        for (UserEntity ue : users) {
            this.removeUser(ue);
        }
    }

    public String getGroupid() {
        return groupid;
    }

    public void setGroupid(String groupid) {
        this.groupid = groupid;
    }
}
